package com.cjzuji.rental.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESUtilsOfThird {
    private static String CHARSET_NAME = "UTF-8";
    //加密 加密模式ECB 填充方式PKCS5PADDING，这里的AES加密使用的是javax.crypto包下的
    public static String encrypt(String input, String key) {
        byte[] crypted = null;
        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(CHARSET_NAME), "AES");

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes(CHARSET_NAME));
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        Base64.Encoder encoder = Base64.getEncoder();

        return encoder.encodeToString(crypted);
    }

    //解密
    public static String decrypt(String input, String key) throws Exception {
        /*byte[] output = null;
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(CHARSET_NAME), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            output = cipher.doFinal(decoder.decode(input.getBytes(CHARSET_NAME)));
        } catch (Exception e) {
            System.out.println("报错了："+e.getMessage());
        }
        return new String(output);*/

        byte[] output = null;

        Base64.Decoder decoder = Base64.getDecoder();
        SecretKeySpec skey = new SecretKeySpec(key.getBytes(CHARSET_NAME), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skey);
        output = cipher.doFinal(decoder.decode(input.getBytes(CHARSET_NAME)));

        return new String(output);
    }

    //AES加密
    public static String encryptAES(String str, String key) {
        if (key == null) {
            //System.out.print("Key为空null");
            System.out.println("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (key.length() != 16) {
            //System.out.print("Key长度不是16位");
            System.out.println("Key长度不是16位");

            return null;
        }
        byte[] encrypted = null;

        try {
            byte[] raw = key.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            encrypted = cipher.doFinal(str.getBytes("utf-8"));
            return new org.apache.commons.codec.binary.Base64().encodeToString(encrypted);//此处使用BASE64做转码功能，同时能起到2次加密的作用。
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    //AES解密
    public static String decryptAES(String str, String key) {

        try {
            // 判断Key是否正确
            if (key == null) {
                //System.out.print("Key为空null");
                System.out.println("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (key.length() != 16) {
                //System.out.print("Key长度不是16位");
                System.out.println("Key长度不是16位");
                return null;
            }
            byte[] raw = key.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new org.apache.commons.codec.binary.Base64().decode(str);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
}
